package com.ibeeking.found.uaa.g.model;

import com.ibeeking.gmc.global.api.common.dto.OauthClientDTO;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @ClassName ClientDetails
 * @Description
 * @Author ibeeking
 * @Date 2020-08-27 15:31
 **/
public class GmcClientDetailsModel implements org.springframework.security.oauth2.provider.ClientDetails {

    private OauthClientDTO oauthClientDTO;

    public GmcClientDetailsModel() {
    }

    public GmcClientDetailsModel(OauthClientDTO oauthClientDTO) {
        this.oauthClientDTO = oauthClientDTO;
    }

    @Override
    public String getClientId() {
        return oauthClientDTO.getClientId();
    }

    @Override
    public Set<String> getResourceIds() {
        return this.strToSet(oauthClientDTO.getResourceIds());
    }

    @Override
    public String getClientSecret() {
        return oauthClientDTO.getClientSecret();
    }

    @Override
    public boolean isSecretRequired() {
        return !StringUtils.isEmpty(oauthClientDTO.getClientSecret());
    }

    @Override
    public boolean isScoped() {
        return !StringUtils.isEmpty(oauthClientDTO.getScope());
    }

    @Override
    public Set<String> getScope() {
        return this.strToSet(oauthClientDTO.getScope());
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        return this.strToSet(oauthClientDTO.getAuthorizedGrantTypes());
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        return this.strToSet(oauthClientDTO.getWebServerRedirectUri());
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        if (StringUtils.isEmpty(oauthClientDTO.getAuthorities())) {
            return Collections.emptyList();
        }
        return JsonUtils.jsonToList(Arrays.asList(oauthClientDTO.getAuthorities().split(",")), GrantedAuthority.class);
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return oauthClientDTO.getAccessTokenValidity();
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return oauthClientDTO.getRefreshTokenValidity();
    }

    public Set<String> getAutoApproveScopes() {
        return this.strToSet(oauthClientDTO.getAutoApprove());
    }

    @Override
    public boolean isAutoApprove(String scope) {
        if (StringUtils.isEmpty(oauthClientDTO.getAutoApprove())) {
            return false;
        }
        Iterator var2 = getAutoApproveScopes().iterator();
        String auto;
        do {
            if (!var2.hasNext()) {
                return false;
            }
            auto = (String) var2.next();
        } while (!auto.equals("true") && !scope.matches(auto));
        return true;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        if (StringUtils.isEmpty(oauthClientDTO.getAdditionalInformation())) {
            return Collections.emptyMap();
        }
        return JSON.parseObject(oauthClientDTO.getAdditionalInformation());
    }

    private Set<String> strToSet(String param) {
        if (StringUtils.isEmpty(param)) {
            return Collections.emptySet();
        }
        return new LinkedHashSet<>(Arrays.asList(param.split(",")));
    }
}
